# Contributor: Mark Riedesel <mark@klowner.com>
# Contributor: Leon Marz <main@lmarz.org>
# Maintainer: Leon Marz <main@lmarz.org>
pkgname=blender
pkgver=3.2.1
_pkgver=${pkgver%.[0-9]}
pkgrel=0
pkgdesc="3D Creation/Animation/Publishing System"
url="https://www.blender.org/"
arch="x86_64 aarch64" # limited by openvdb
license="GPL-2.0-or-later"
depends="blender-shared=$pkgver-r$pkgrel"
makedepends="cmake
	alembic-dev
	blosc-dev
	boost-dev
	eigen-dev
	embree-dev
	embree-static
	ffmpeg-dev
	fftw-dev
	freetype-dev
	glew-dev
	gmp-dev
	jack-dev
	libharu-dev
	libjpeg-turbo-dev
	libpng-dev
	libsndfile-dev
	libtbb-dev
	libx11-dev
	libxi-dev
	libxrender-dev
	llvm-dev
	lzo-dev
	openal-soft-dev
	opencolorio-dev
	openexr-dev
	openimageio-dev
	openjpeg-dev
	opensubdiv-dev
	openvdb-dev
	openvdb-nanovdb
	openxr-dev
	osl
	osl-dev
	potrace-dev
	pugixml-dev
	pulseaudio-dev
	py3-numpy-dev
	python3-dev
	samurai
	sdl2-dev
	tiff-dev
	"
case "$CARCH" in
	"x86_64") makedepends="$makedepends openimagedenoise-dev";;
esac
subpackages="$pkgname-doc $pkgname-shared::noarch $pkgname-headless py3-$pkgname:python"
source="https://download.blender.org/source/blender-$pkgver.tar.xz
	0001-musl-fixes.patch
	0002-fix-linking-issue.patch
	0003-increase-thread-stack-size-for-musl.patch
	"


build() {
	# Headless
	mkdir -p "$builddir"/build-headless
	cd "$builddir"/build-headless
	_build -C../build_files/cmake/config/blender_headless.cmake

	# Full
	mkdir -p "$builddir"/build-full
	cd "$builddir"/build-full
	_build -C../build_files/cmake/config/blender_full.cmake

	# Python module
	mkdir -p "$builddir"/build-py
	cd "$builddir"/build-py
	_build -C../build_files/cmake/config/bpy_module.cmake
}

_build() {
	local _py_version=$(python3 -c 'import sys; print("%i.%i" % (sys.version_info.major, sys.version_info.minor))')

	export CFLAGS="$CFLAGS -D__MUSL__"
	export CXXFLAGS="$CXXFLAGS -D__MUSL__"

	cmake .. "$@" \
		-G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=Release \
		-DWITH_PYTHON_INSTALL=OFF \
		-DWITH_INSTALL_PORTABLE=OFF \
		-DWITH_SYSTEM_LZO=ON \
		-DWITH_SYSTEM_EIGEN3=ON \
		-DWITH_SYSTEM_GLEW=ON \
		-DPYTHON_VERSION=$_py_version

	cmake --build .
}

package() {
	# Install headless files
	cd "$builddir"/build-headless
	DESTDIR="$pkgdir"/headless cmake --install .

	# Rename the headless blender to blender-headless
	mkdir -p "$pkgdir"/usr/bin
	mv "$pkgdir"/headless/usr/bin/blender "$pkgdir"/usr/bin/blender-headless
	rm -rf "$pkgdir"/headless

	# Install python module
	cd "$builddir"/build-py
	DESTDIR="$pkgdir" cmake --install .

	# Install the full package
	cd "$builddir"/build-full
	DESTDIR="$pkgdir" cmake --install .
}

shared() {
	pkgdesc="Blender shared runtime data and add-on scripts"
	depends=""
	mkdir -p "$subpkgdir"/usr/share/
	mv "$pkgdir"/usr/share/blender "$subpkgdir"/usr/share/
}

headless() {
	pkgdesc="$pkgdesc (headless build)"
	depends="blender-shared=$pkgver-r$pkgrel"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/blender-headless "$subpkgdir"/usr/bin/
}

python() {
	local _py_version=$(python3 -c 'import sys; print("%i.%i" % (sys.version_info.major, sys.version_info.minor))')

	pkgdesc="Blender modules for Python 3"
	mkdir -p "$subpkgdir"/usr/lib/python"$_py_version"/site-packages
	mv "$pkgdir"/usr/lib/python"$_py_version"/site-packages/bpy.so "$subpkgdir"/usr/lib/python"$_py_version"/site-packages/
	rm -rf "$pkgdir"/usr/lib

	# Symlink to the blender-shared files
	ln -s /usr/share/blender/"$_pkgver" "$subpkgdir"/usr/lib/python"$_py_version"/site-packages/"$_pkgver"
}

sha512sums="
fb496c47b2aebd403e00954d55d140ee9f0dc7733e1013b1665f9db83a132ad63a09226edc662871c8c1b6e2fbd8870954d86cb1324d0c44230f79d2b46aecef  blender-3.2.1.tar.xz
05074e900e70a5acc1fb6ef85afcdda6cb1e2546544fd78c09502d5b75e4e52a083cdaf4d71887c6f1fceaba1603dd138f1fa8e4d5a2fb826b955f6d27b0a81f  0001-musl-fixes.patch
0a9fd519c8bbcfca121f158e0d78224b55412cb704ca586564d77409582336c1ed2681ff0b8e8b103d6fbe8fd6a0cc9697447ffaefb90c90ee458273fbdd7fd5  0002-fix-linking-issue.patch
27704667a06577847432df3c55de07a7d07d34f076b24db243c11c21d9d6a485367b0f074ab652bc9a85ccefcb0e6a134a09a896f980a9652ee06fc1df30ea97  0003-increase-thread-stack-size-for-musl.patch
"
